{
  "cells": [
    {
      "attachments": {},
      "cell_type": "markdown",
      "id": "txooXPfhaCK0",
      "metadata": {
        "id": "txooXPfhaCK0"
      },
      "source": [
        "# ART GRAPHITE Whitebox Attack"
      ]
    },
    {
      "attachments": {},
      "cell_type": "markdown",
      "id": "CS3f9y-iq2rW",
      "metadata": {
        "id": "CS3f9y-iq2rW"
      },
      "source": [
        "Install Kornia, an open source Differentiable Computer Vision Library for PyTorch\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "Xnp0CXChiuI1",
      "metadata": {
        "id": "Xnp0CXChiuI1"
      },
      "outputs": [],
      "source": [
        "pip install kornia"
      ]
    },
    {
      "attachments": {},
      "cell_type": "markdown",
      "id": "ym2ZL1AEqzqM",
      "metadata": {
        "id": "ym2ZL1AEqzqM"
      },
      "source": [
        "Setup and load the data and train a model to test on."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "AO9XssuBaCK6",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "AO9XssuBaCK6",
        "outputId": "8fde263e-3024-497a-8e0a-d7f6c143cf0a"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "Accuracy on benign test examples: 97.6%\n"
          ]
        }
      ],
      "source": [
        "import torch\n",
        "import torch.nn as nn\n",
        "import torch.nn.functional as F\n",
        "import torch.optim as optim\n",
        "import matplotlib.pyplot as plt\n",
        "import numpy as np\n",
        "\n",
        "from art.attacks.evasion import GRAPHITEWhiteboxPyTorch\n",
        "from art.estimators.classification import PyTorchClassifier\n",
        "from art.utils import load_mnist\n",
        "\n",
        "\n",
        "# Step 1: Load the MNIST dataset\n",
        "\n",
        "(x_train, y_train), (x_test, y_test), min_pixel_value, max_pixel_value = load_mnist()\n",
        "\n",
        "# Step 1a: Swap axes to PyTorch's NCHW format\n",
        "\n",
        "x_train = np.transpose(x_train, (0, 3, 1, 2)).astype(np.float32)\n",
        "x_test = np.transpose(x_test, (0, 3, 1, 2)).astype(np.float32)\n",
        "\n",
        "# Step 2: Create the model\n",
        "\n",
        "model = nn.Sequential(\n",
        "    nn.Conv2d(1, 4, 5), nn.ReLU(), nn.MaxPool2d(2, 2),\n",
        "    nn.Conv2d(4, 10, 5), nn.ReLU(), nn.MaxPool2d(2, 2),\n",
        "    nn.Flatten(),\n",
        "    nn.Linear(4*4*10, 100),\n",
        "    nn.Linear(100, 10)\n",
        ")\n",
        "# Step 2a: Define the loss function and the optimizer\n",
        "\n",
        "criterion = nn.CrossEntropyLoss()\n",
        "optimizer = optim.Adam(model.parameters(), lr=0.01)\n",
        "\n",
        "# Step 3: Create the ART classifier\n",
        "\n",
        "classifier = PyTorchClassifier(\n",
        "    model=model,\n",
        "    clip_values=(min_pixel_value, max_pixel_value),\n",
        "    loss=criterion,\n",
        "    optimizer=optimizer,\n",
        "    input_shape=(1, 28, 28),\n",
        "    nb_classes=10,\n",
        ")\n",
        "\n",
        "# Step 4: Train the ART classifier\n",
        "\n",
        "classifier.fit(x_train, y_train, batch_size=64, nb_epochs=5)\n",
        "\n",
        "# Step 5: Evaluate the ART classifier on benign test examples\n",
        "\n",
        "predictions = classifier.predict(x_test)\n",
        "accuracy = np.sum(np.argmax(predictions, axis=1) == np.argmax(y_test, axis=1)) / len(y_test)\n",
        "print(\"Accuracy on benign test examples: {}%\".format(accuracy * 100))"
      ]
    },
    {
      "attachments": {},
      "cell_type": "markdown",
      "id": "wsOpmCgAaCK9",
      "metadata": {
        "id": "wsOpmCgAaCK9"
      },
      "source": [
        "# Get Init Image\n",
        "\n",
        "Second, load an initialization image to attack."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "qAKzUShfaCK9",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 465
        },
        "id": "qAKzUShfaCK9",
        "outputId": "1626a4f0-1918-4fac-b2dc-e2ebf7719e37"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "Init image is:  7\n"
          ]
        },
        {
          "data": {
            "image/png": "iVBORw0KGgoAAAANSUhEUgAAAaAAAAGdCAYAAABU0qcqAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjcuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/bCgiHAAAACXBIWXMAAA9hAAAPYQGoP6dpAAAbKUlEQVR4nO3df3DU9b3v8dcCyQqYbAwh2UQCBvxBFUinFNJclMaSS4hnGFDOHVBvBxwvXGlwhNTqiaMgbeemxTno0UPxnxbqGQHLuQJHTi8djSaMbYKHKIfLtWZIJhYYklBzD9kQJATyuX9wXV1JwO+ym3eyPB8z3xmy+/3k+/br6pNvsvnG55xzAgBggA2zHgAAcH0iQAAAEwQIAGCCAAEATBAgAIAJAgQAMEGAAAAmCBAAwMQI6wG+rre3VydPnlRKSop8Pp/1OAAAj5xz6uzsVE5OjoYN6/86Z9AF6OTJk8rNzbUeAwBwjY4fP65x48b1+/ygC1BKSook6W7dpxFKMp4GAODVBfXoff0+/P/z/sQtQJs2bdILL7yg1tZW5efn65VXXtHMmTOvuu6LL7uNUJJG+AgQAAw5//8Oo1f7Nkpc3oTwxhtvqLy8XOvWrdOHH36o/Px8lZSU6NSpU/E4HABgCIpLgDZu3Kjly5frkUce0Z133qlXX31Vo0aN0m9+85t4HA4AMATFPEDnz59XfX29iouLvzzIsGEqLi5WbW3tZft3d3crFApFbACAxBfzAH322We6ePGisrKyIh7PyspSa2vrZftXVlYqEAiEN94BBwDXB/MfRK2oqFBHR0d4O378uPVIAIABEPN3wWVkZGj48OFqa2uLeLytrU3BYPCy/f1+v/x+f6zHAAAMcjG/AkpOTtb06dNVVVUVfqy3t1dVVVUqLCyM9eEAAENUXH4OqLy8XEuXLtV3v/tdzZw5Uy+99JK6urr0yCOPxONwAIAhKC4BWrx4sf76179q7dq1am1t1be//W3t27fvsjcmAACuXz7nnLMe4qtCoZACgYCKtIA7IQDAEHTB9ahae9TR0aHU1NR+9zN/FxwA4PpEgA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nfNPv8rzmX//ln+IwyeX+U0WZ5zVpr/X94w9APF1wParWHnV0dCg1NbXf/bgXHADABAECAJggQAAAEwQIAGCCAAEATBAgAIAJAgQAMEGAAAAmCBAAwAQBAgCYIEAAABMECABgggABAExE9RtRgcFu+J23R7VuxY49MZ6kb3f+xvudrW/5p7o4TALY4QoIAGCCAAEATBAgAIAJAgQAMEGAAAAmCBAAwAQBAgCYIEAAABMECABgggABAEwQIACACQIEADDBzUiRkD750U1RrZs/KhTjSfo2rvq890XOxX4QwBBXQAAAEwQIAGCCAAEATBAgAIAJAgQAMEGAAAAmCBAAwAQBAgCYIEAAABMECABgggABAEwQIACACW5GikHv3PyZntdUzf/7KI82Ksp1ALziCggAYIIAAQBMECAAgAkCBAAwQYAAACYIEADABAECAJggQAAAEwQIAGCCAAEATBAgAIAJAgQAMMHNSDHonZw13POa8SMG7qair3dmel6TFDrveY3zvAIY3LgCAgCYIEAAABOeAlRZWakZM2YoJSVFmZmZWrhwoRoaGiL2KSoqks/ni9gee+yxmA4NABj6PAWopqZGZWVlqqur09tvv62enh7NnTtXXV1dEfstX75cLS0t4W3Dhg0xHRoAMPR5ehPCvn37Ij7eunWrMjMzVV9fr9mzZ4cfHzVqlILBYGwmBAAkpGv6HlBHR4ckKT09PeLx119/XRkZGZoyZYoqKip09uzZfj9Hd3e3QqFQxAYASHxRvw27t7dXq1ev1qxZszRlypTw4w899JAmTJignJwcHT58WE8//bQaGhr05ptv9vl5KisrtX79+mjHAAAMUVEHqKysTEeOHNH7778f8fiKFSvCf546daqys7M1Z84cNTU1adKkSZd9noqKCpWXl4c/DoVCys3NjXYsAMAQEVWAVq1apb1792r//v0aN27cFfctKCiQJDU2NvYZIL/fL7/fH80YAIAhzFOAnHN6/PHHtWvXLlVXVysvL++qaw4dOiRJys7OjmpAAEBi8hSgsrIybdu2TXv27FFKSopaW1slSYFAQCNHjlRTU5O2bdum++67T2PGjNHhw4e1Zs0azZ49W9OmTYvLPwAAYGjyFKDNmzdLuvTD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cffeQ++ugjJ8lt3LjRffTRR+4vf/mLc865X/ziFy4tLc3t2bPHHT582C1YsMDl5eW5zz//3Hjy2LrSeejs7HRPPvmkq62tdc3Nze6dd95x3/nOd9xtt93mzp07Zz16zKxcudIFAgFXXV3tWlpawtvZs2fD+zz22GNu/Pjx7t1333UHDx50hYWFrrCw0HDq2LvaeWhsbHQ//elP3cGDB11zc7Pbs2ePmzhxops9e7bx5JGGRICcc+6VV15x48ePd8nJyW7mzJmurq7OeqQBt3jxYpedne2Sk5PdzTff7BYvXuwaGxutx4q79957z0m6bFu6dKlz7tJbsZ977jmXlZXl/H6/mzNnjmtoaLAdOg6udB7Onj3r5s6d68aOHeuSkpLchAkT3PLlyxPuL2l9/fNLclu2bAnv8/nnn7sf/ehH7qabbnKjRo1y999/v2tpabEbOg6udh6OHTvmZs+e7dLT053f73e33nqr+8lPfuI6OjpsB/8afh0DAMDEoP8eEAAgMREgAIAJAgQAMEGAAAAmCBAAwAQBAgCYIEAAABMECABgggABAEwQIACACQIEADBBgAAAJv4fx1BnJzDsp98AAAAASUVORK5CYII=",
            "text/plain": [
              "<Figure size 640x480 with 1 Axes>"
            ]
          },
          "metadata": {},
          "output_type": "display_data"
        },
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "Target label is:  2\n"
          ]
        }
      ],
      "source": [
        "init_image = x_test[0, :, :, :]\n",
        "\n",
        "print(\"Init image is: \", np.argmax(classifier.predict(np.array([init_image]))[0]))\n",
        "plt.imshow((init_image.squeeze() * 255).astype(np.uint))\n",
        "plt.show()\n",
        "\n",
        "target_label_index = np.argmax(y_test[1])\n",
        "target_label = target_label_index.item()\n",
        "print(\"Target label is: \", target_label)"
      ]
    },
    {
      "attachments": {},
      "cell_type": "markdown",
      "id": "wvslQXLWaCK-",
      "metadata": {
        "id": "wvslQXLWaCK-"
      },
      "source": [
        "# Run GRAPHITE Whitebox Attack\n",
        "\n",
        "Finally, run an attack on a GRAHPITEWhiteboxPyTorch instance. We set the network size to 28 x 28 for MNIST and set the inner EoT PGD steps to 50. The attack is shown at the end."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "KSbz8rOhaCK-",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 447
        },
        "id": "KSbz8rOhaCK-",
        "outputId": "94bbbbc9-4dae-4972-b029-b29b98805e87",
        "scrolled": true
      },
      "outputs": [
        {
          "data": {
            "image/png": "iVBORw0KGgoAAAANSUhEUgAAAaAAAAGdCAYAAABU0qcqAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjcuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/bCgiHAAAACXBIWXMAAA9hAAAPYQGoP6dpAAAccUlEQVR4nO3df3BUdZrv8U9DkhYwaQwhvyRgwB+oQKxhIGZRBocUIW5RoNxdUHcLLC9cmGAJGX9MLBWZmVuZwSpkdRBrq2Zg3BJw3CswMnuZ0mDCVRNmQBiK65giqTjAkISRXbohSAjke//g2toSxNN05+kO71fVqUqfc54+D18OfHK6T3/b55xzAgCgl/WzbgAAcHUigAAAJgggAIAJAggAYIIAAgCYIIAAACYIIACACQIIAGAixbqBr+vu7tbRo0eVnp4un89n3Q4AwCPnnE6ePKn8/Hz163fp65yEC6CjR4+qoKDAug0AwBU6fPiwhg0bdsntCRdA6enpkqS7dK9SlGrcDQDAq3Pq0vv6j/D/55cStwBas2aNXnjhBbW1tamoqEgvv/yyJk6ceNm6L152S1GqUnwEEAAknf8/w+jl3kaJy00Ib7zxhiorK7V8+XJ99NFHKioqUllZmY4dOxaPwwEAklBcAmjVqlVasGCBHn74Yd1222169dVXNXDgQP3qV7+Kx+EAAEko5gF09uxZ7dmzR6WlpV8epF8/lZaWqr6+/qL9Ozs7FQqFIhYAQN8X8wD67LPPdP78eeXk5ESsz8nJUVtb20X7V1dXKxAIhBfugAOAq4P5B1GrqqoUDAbDy+HDh61bAgD0gpjfBZeVlaX+/furvb09Yn17e7tyc3Mv2t/v98vv98e6DQBAgov5FVBaWprGjx+vmpqa8Lru7m7V1NSopKQk1ocDACSpuHwOqLKyUvPmzdN3v/tdTZw4UatXr1ZHR4cefvjheBwOAJCE4hJAc+bM0d/+9jc999xzamtr0x133KHt27dfdGMCAODq5XPOOesmvioUCikQCGiKZjITAgAkoXOuS7XaqmAwqIyMjEvuZ34XHADg6kQAAQBMEEAAABMEEADABAEEADBBAAEATBBAAAATBBAAwAQBBAAwQQABAEwQQAAAEwQQAMAEAQQAMEEAAQBMEEAAABMEEADABAEEADBBAAEATBBAAAATBBAAwAQBBAAwQQABAEwQQAAAEwQQAMAEAQQAMEEAAQBMEEAAABMEEADABAEEADBBAAEATBBAAAATBBAAwAQBBAAwQQABAEwQQAAAEwQQAMAEAQQAMEEAAQBMEEAAABMEEADABAEEADBBAAEATBBAAAATBBAAwAQBBAAwQQABAEwQQAAAEwQQAMAEAQQAMEEAAQBMEEAAABMEEADABAEEADBBAAEATBBAAAATMQ+g559/Xj6fL2IZPXp0rA8DAEhyKfF40ttvv13vvvvulwdJicthAABJLC7JkJKSotzc3Hg8NQCgj4jLe0AHDx5Ufn6+Ro4cqYceekiHDh265L6dnZ0KhUIRCwCg74t5ABUXF2v9+vXavn271q5dq5aWFt199906efJkj/tXV1crEAiEl4KCgli3BABIQD7nnIvnAU6cOKERI0Zo1apVeuSRRy7a3tnZqc7OzvDjUCikgoICTdFMpfhS49kaACAOzrku1WqrgsGgMjIyLrlf3O8OGDx4sG6++WY1NTX1uN3v98vv98e7DQBAgon754BOnTql5uZm5eXlxftQAIAkEvMAevzxx1VXV6dPP/1UH374oe677z71799fDzzwQKwPBQBIYjF/Ce7IkSN64IEHdPz4cQ0dOlR33XWXGhoaNHTo0FgfCgCQxGIeQJs2bYr1UwIA+iDmggMAmCCAAAAmCCAAgAkCCABgggACAJgggAAAJgggAIAJAggAYIIAAgCYIIAAACYIIACACQIIAGAi7l9Ih951fEGJ55rh/9zzlwVezifHcjzXnO30/i2312/0XlP3r//quSZar58c4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okWLYto0ACD5eQqguro6VVRUqKGhQe+88466uro0bdo0dXR0ROy3YMECtba2hpeVK1fGtGkAQPLzdBPC9u3bIx6vX79e2dnZ2rNnjyZPnhxeP3DgQOXm5samQwBAn3RF7wEFg0FJUmZmZsT6119/XVlZWRozZoyqqqp0+vTpSz5HZ2enQqFQxAIA6Puivg27u7tbS5cu1aRJkzRmzJjw+gcffFAjRoxQfn6+9u/fr6eeekqNjY166623enye6upqrVixIto2AABJKuoAqqio0IEDB/T+++9HrF+4cGH457FjxyovL09Tp05Vc3OzRo0addHzVFVVqbKyMvw4FAqpoKAg2rYAAEkiqgBasmSJtm3bpp07d2rYsGHfuG9xcbEkqampqccA8vv98vv90bQBAEhingLIOadHH31UmzdvVm1trQoLCy9bs2/fPklSXl5eVA0CAPomTwFUUVGhDRs2aOvWrUpPT1dbW5skKRAIaMCAAWpubtaGDRt07733asiQIdq/f7+WLVumyZMna9y4cXH5AwAAkpOnAFq7dq2kCx82/ap169Zp/vz5SktL07vvvqvVq1ero6NDBQUFmj17tp555pmYNQwA6Bs8vwT3TQoKClRXV3dFDQEArg7Mhg18RfXx2zzXzM74yHPNrWneZ+tu7jrluWZU6rWea6ToZtHuizNor/70Q881S2/4uzh00jcxGSkAwAQBBAAwQQABAEwQQAAAEwQQAMAEAQQAMEEAAQBMEEAAABMEEADABAEEADBBAAEATBBAAAATTEaKhDfyR/Wea+790Xfi0EnP/o96Z/LJYz/wfpy9z7wSh06uHtFMGotvjysgAIAJAggAYIIAAgCYIIAAACYIIACACQIIAGCCAAIAmCCAAAAmCCAAgAkCCABgggACAJhIuLngnHOSpHPqkpxxM0ACOX/2jOea0MnuOHTSs3Ouq9eO1VuiGb++OA5endOFMfji//NL8bnL7dHLjhw5ooKCAus2AABX6PDhwxo2bNgltydcAHV3d+vo0aNKT0+Xz+eL2BYKhVRQUKDDhw8rIyPDqEN7jMMFjMMFjMMFjMMFiTAOzjmdPHlS+fn56tfv0u/0JNxLcP369fvGxJSkjIyMq/oE+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AB9EwEEADBBAAEATBBAAAATBBAAwAQBBAAwQQABAEwQQAAAEwQQAMAEAQQAMEEAAQBMEEAAABP/D09LweNxE97jAAAAAElFTkSuQmCC",
            "text/plain": [
              "<Figure size 640x480 with 1 Axes>"
            ]
          },
          "metadata": {},
          "output_type": "display_data"
        },
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "Predicted label is:  2\n"
          ]
        }
      ],
      "source": [
        "# Step 6: Generate adversarial test examples\n",
        "attack = GRAPHITEWhiteboxPyTorch(\n",
        "    classifier,\n",
        "    (28, 28),\n",
        "    patch_removal_size = 4,\n",
        "    patch_removal_interval = 1,\n",
        "    steps = 50\n",
        ")\n",
        "\n",
        "x_test_adv = attack.generate(x=x_test[0, :, :, :][np.newaxis, :, :, :], y=y_test[1][np.newaxis, :], mask=None)\n",
        "plt.imshow(x_test_adv[0,...].squeeze())\n",
        "plt.show()\n",
        "\n",
        "predicted_label = np.argmax(classifier.predict(np.array([x_test_adv[0, ...]]))[0])\n",
        "print(\"Predicted label is: \", predicted_label)"
      ]
    }
  ],
  "metadata": {
    "colab": {
      "gpuType": "T4",
      "provenance": []
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.8.10"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}
